$(function(){
	$(".mc").css("display","none");
	$('#categorys').hover(function(){$(this).children('.mc').toggle();});
	var toUrl = $Util.getQueryString(document.location.search,'to_url');
	if(toUrl.length !=0){
		$('#login_message').html('您需要登录后才能继续浏览或操作！');
		$('#login_info').show();
	}
	$('#sub').bind('click',subFrm);
	remember();
//	drawValidateCode();
});

function remember(){
	var flag = $.cookie('remember');
	if(flag){
		$("#REMEMBER").attr("checked",true);
		$('#LOGIN_NAME').val($.cookie('name'));
		$('#PASSWORD').val($.cookie('pwd'));
	}
}

function drawValidateCode() {
	var vc = new ValidateCode('/validateCode.jsp',60,18,4, 'MEMBER_LOGIN');
	vc.applyTo('validateCode');
}

function validateAccount(){
	$('#login_info').hide();
	var loginName = $('#LOGIN_NAME');
	var loginNameVal = $.trim(loginName.val());
	if(loginNameVal.length == 0){
		$('#login_message').html('请输入账号');
		$('#login_info').show();
		return false;
	}
	return true;
}

function validatePassword(){
	$('#login_info').hide();
	var pwd = $('#PASSWORD');
	var pwdVal = $.trim(pwd.val());
	if(pwdVal.length == 0){
		$('#login_message').html('请输入密码');
		$('#login_info').show();
		return false;
	}
	return true;
}

function validateCode(){
	var vcode = $('#VALIDATE_CODE');
	var vcodeTip = $('#VALIDATE_CODE_TIP');
	var vcodeVal = $.trim(vcode.val());
	if(vcodeVal.length == 0){
		vcodeTip.html('请输入验证码');
		vcodeTip.attr('class', 'error_tip');
		return false;
	}
	else{
		vcodeTip.html('');
	}
	return true;
}

function validateResult(){
	return validateAccount() && validatePassword();
}

function subFrm(){
	var options = {
	    dataType: 'json',
	    beforeSubmit: validateResult,
	    success: function(data) {
	    	if(data.success){
	    		var flag = $("#REMEMBER").attr("checked");
	    		if(flag){
	    			var account = $('#LOGIN_NAME').val();
	    			var pwd = $('#PASSWORD').val();
	    			$.cookie('name', account, {expires:365});
	    			$.cookie('pwd', pwd, {expires:365});
	    		}
	    		$.cookie('remember', flag);
	    		
	    		var toUrl = $Util.getQueryString(document.location.search,'to_url');
	    		if(toUrl.length == 0){
	    			window.location.href="../index.html";
	    		} else{
	    			window.location.href = toUrl;
	    		}
	      		
	      	}else{
	      		$('#login_message').html(data.errors.errmsg);
				$('#login_info').show();
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

function listenerPasswordKeyDown(){
	var e = e || event;
	var currKey = e.keyCode||e.which||e.charCode;
	if(currKey == 13){
		subFrm();
	}
}

function listenerNameKeyDown(){
	var e = e || event;
	var currKey = e.keyCode||e.which||e.charCode;
	if(currKey == 13){
		$('#PASSWORD').focus();
	}
}